import axios from "axios"
import Nprogress from "nprogress"
import "nprogress/nprogress.css"

import { getUserTempId } from "../utils/uuid"

const ajax = axios.create({
  baseURL: "http://39.98.123.211/",
  timeout: 5000,
})

ajax.interceptors.request.use((config) => {
  // 开启网络请求进度条
  Nprogress.start()
  // 获取用户的临时id
  config.headers.userTempId = getUserTempId()
  return config
})

ajax.interceptors.response.use(
  (response) => {
    // 网络请求结束进度条
    Nprogress.done()
    return response.data
  },
  (error) => {
    Nprogress.done()
    return Promise.reject(new Error(error.message))
  }
)

export default ajax
